package com.gancp.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.concurrent.TimeUnit;

/**
 * Created by GCP on 2018/5/22.
 */
public class RedisManager {

    private RedisTemplate<String, Object> redisTemplate;

    /*****************************************************************基本操作方法***************************************************************/
    /**
     * @Author gancp
     * @Date 时间被黑洞吸走了
     * @Descripition 指定缓存失效时间
     **/
    public boolean expire(String key, long seconds) {
        if (seconds > 0) {
            redisTemplate.expire( key, seconds, TimeUnit.SECONDS );
            return true;
        }
        return false;
    }

    public long getExpire(String key) {
        return redisTemplate.getExpire( key );
    }

    /**
     * 判断key是否存在
     *
     * @param key 键
     * @return true 存在 false不存在
     */
    public boolean hasKey(String key) {
        try {
            return redisTemplate.hasKey( key );
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 删除缓存
     *
     * @param key 可以传一个值 或多个
     */
    public void del(String... key) {
        if (key != null && key.length > 0) {
            if (key.length == 1) {
                redisTemplate.delete( key[0] );
            } else {
                redisTemplate.delete( CollectionUtils.arrayToList( key ) );
            }
        }
    }

    /***********************************************************操作String************************************************************/

    /**
     * @Author gancp
     * @Date 时间被黑洞吸走了
     * @Descripition 放入缓存
     **/
    public void set(String key, Object value) {
        redisTemplate.opsForValue().set( key, value );
    }

    /**
     * @Author gancp
     * @Date 时间被黑洞吸走了
     * @Descripition 设置带失效时间的
     **/
    public void set(String key, Object value, long seconds) {
        redisTemplate.opsForValue().set( key, value, seconds, TimeUnit.SECONDS );
    }

    /**
     * @Author gancp
     * @Date 时间被黑洞吸走了
     * @Descripition 读取
     **/
    public Object get(String key) {
        return redisTemplate.opsForValue().get( key );
    }

    public RedisTemplate<String, Object> getRedisTemplate() {
        return redisTemplate;
    }

    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
        RedisSerializer stringSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer( stringSerializer );
        redisTemplate.setValueSerializer( stringSerializer );
        redisTemplate.setHashKeySerializer( stringSerializer );
        redisTemplate.setHashValueSerializer( stringSerializer );
        this.redisTemplate = redisTemplate;
    }
}
